layui.use(['table','layer','admin', 'ax','laydate','ztree'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var $ZTree = layui.ztree;
    var layer = layui.layer;

    /**
     * 订单信息管理
     */
    var Order = {
        tableId: "orderTable"
        , condition: {
            name: "",
            deptId: "",
            timeLimit: ""
        }
    };
    /**
     * 初始化表格的列
     */
    Order.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true,hide:'true', title: 'id'},
            {field: 'orderNo', sort: true, align:'center', title: '订单号',event:'orderEdit',
                templet: function(d){
                    return '<span style="color: #01AAED;">'+ d.orderNo +'</span>'
                }
            },
            {field: 'playerId', sort: true,hide:'true',  align:'center', title: '购买人ID'},
            {field: 'trxid', sort: true, align:'center', title: '支付编号'},
            {field: 'mergeOrder', sort: true, align:'center', title: '订单类型',templet:"#mergeTpl"},
            {field: 'userNo', sort: true,  align:'center', title: '用户编号'},
            {field: 'userName', sort: true,  align:'center',title: '购买人'},
            {field: 'totalPrice', sort: true, align:'center', title: '订单总金额(元)',
                totalRow:'true',totalRowText:'总计',
                templet:function (d) {
                   return Feng.formatCurrency(d.totalPrice);
                }
            },
            {field: 'payPrice',hide:'true', sort: true, align:'center', title: '支付现金(元)',
                templet:function (d) {
                    return Feng.formatCurrency(d.payPrice);
                }
            },
            {field: 'payScore', sort: true, align:'center',hide:'true', title: '支付积分'},
            {field: 'payScorePrice', align:'center', sort: true,hide:'true', title: '积分抵扣金额(元)',
                templet:function (d) {
                    return Feng.formatCurrency(d.payScorePrice);
                }
            },
            {field: 'deductionPrice', sort: true,  align:'center',title: '优惠券抵扣金额(元)',
                templet:function (d) {
                    return Feng.formatCurrency(d.deductionPrice);
                }
            },
            {field: 'shipFee', sort: true,  align:'center',title: '运费(元)'},
            {field: 'receiverName', align:'center', sort: true,hide:'true', title: '收货人'},
            {field: 'receiverPhone', align:'center', sort: true,hide:'true', title: '收货电话'},
            {field: 'receiverAddr', sort: true, align:'center', hide:'true',title: '收货地址'},
            {field: 'shipCompany', sort: true,hide:'true',  align:'center',title: '物流公司'},
            {field: 'shipNo', sort: true, align:'center', hide:'true',title: '物流单号'},
            {field: 'agentId', sort: true, align:'center',hide:'true', title: '代理商id'},
            {field: 'agentName', sort: true, align:'center',hide:'true', title: '代理商'},
            {field: 'deptid', sort: true,hide:'true', align:'center', title: '部门id'},
            {field: 'payTypeName', sort: true, align:'center', title: '支付方式'},
            {field: 'payTime', sort: true, align:'center',title: '支付时间'},
            {field: 'createTime', sort: true,align:'center', title: '新增时间'},
            {field: 'shipTime', sort: true, align:'center',hide:'true',title: '发货时间'},
            {field: 'receiveTime', align:'center',sort: true,hide:'true', title: '收货时间'},
            {field: 'deptName', align:'center',sort: true, title: '所属部门'},
            {field: 'updateTime',align:'center', sort: true, hide:'true',title: '修改时间'},
            {field: 'recordStatus', align:'center',sort: true,hide:'true', title: '记录状态'},
            {field: 'remark', sort: true,  align:'center',hide:'true',title: '备注'},
            {field: 'orderStatus', sort: true, align:'center', title: '订单状态',
                templet:function (d) {
                    console.log(d);
                    return Feng.addBackColor4Enum(d.orderStatus,d.orderStatusName);
                }
            },
            {align: 'center', toolbar: '#tableBar', title: '操作',}
        ]];
    };

    /**
     * 选择部门时
     */
   /* Order.onClickDept = function (e, treeId, treeNode) {
        Order.condition.deptId = treeNode.id;
        Order.search();
    };

    //初始化左侧部门树
    var ztree = new $ZTree("deptTree", "/dept/tree");
    ztree.bindOnClick(Order.onClickDept);
    ztree.init();*/


    laydate.render({
        elem: '#payTimeMin',
        max: Feng.currentDate()
    });

    laydate.render({
        elem: '#payTimeMax',
        max: Feng.currentDate()
    });


    /**
     * 弹出添加对话框
     */
    Order.openAddDlg = function () {
        admin.putTempData('formOk', false);
        layer.open({
            type: 2,
            title: '添加订单信息',
            maxmin:"true",
            content: Feng.ctxPath + '/order/add',
            end: function () {
                admin.getTempData('formOk') && table.reload(Order.tableId);
            }
        });
    };

    /**
     * 导出excel按钮
     */
    Order.exportExcel = function () {
        var checkRows = table.checkStatus(Order.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {

            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    Order.openEditDlg = function (data) {
        admin.putTempData('formOk', false);
        layer.open({
            type: 2,
            title: '修改订单信息',
            area: ['70%', '70%'],
            maxmin:"true",
            content: Feng.ctxPath + '/order/edit?id=' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(Order.tableId);
            }
        });
       // Feng.newCrontab(Feng.ctxPath + '/order/edit?id=' + data.id,"订单修改");
    };

    Order.openDetailDlg = function (data) {
        admin.putTempData('formOk', false);
        layer.open({
            type: 2,
            title: '订单详细信息',
            area: ['70%', '70%'],
            maxmin:'true',
            content: Feng.ctxPath + '/order/order_detail?id=' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(Order.tableId);
            }
        });
    };


    var payTimeMax = null
    var payTimeMin = null
    var totalPriceMin = null
    var totalPriceMax = null
    /**
     * 点击查询按钮
     */
    Order.search = function () {
        var queryData = {};
        queryData['deptId'] = Order.condition.deptId;
        queryData['userName'] = $("#userName").val();
        queryData['userNo'] = $("#userNo").val();
        queryData['orderNo'] = $("#orderNo").val();
        queryData['payNo'] = $("#payNo").val();
        queryData['payType'] = $("#payType").val();
        queryData['orderStatus'] = $("#orderStatus").val();
        Order.handleData()

        queryData['payTimeMax'] = payTimeMax;
        queryData['payTimeMin'] = payTimeMin;
        queryData['totalPriceMin'] = totalPriceMin;
        queryData['totalPriceMax'] = totalPriceMax;

        table.reload(Order.tableId, {where: queryData
            ,page: {
                curr: 1 //重新从第 1 页开始
            }
        });
    };
    Order.handleData=function ( ) {
         payTimeMax = $("#payTimeMax").val();
         payTimeMin = $("#payTimeMin").val();
         totalPriceMin = $("#totalPriceMin").val();
         totalPriceMax = $("#totalPriceMax").val();
           if(!((payTimeMax==""&&payTimeMin=="")||(payTimeMax!="" && payTimeMin!=""))){
               if(payTimeMin==""){
                    payTimeMin="2000-01-01"
               }
               if(payTimeMax==""){
                   payTimeMax=Feng.currentDate();
               }
           }
           if(!((totalPriceMin==""&&totalPriceMax=="")||(totalPriceMax!="" && totalPriceMin!=""))){
               if(totalPriceMin==""){
                   totalPriceMin=0
               }
               if(totalPriceMax==""){
                   totalPriceMax=1000000000;
               }
           }
    }


    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Order.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/order/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Order.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Order.tableId,
        url: Feng.ctxPath + '/order/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        toolbar:'true',
        defaultToolbar: ['filter','exports'],
        cols: Order.initColumn( )
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Order.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Order.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Order.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + Order.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'orderEdit') {
            Order.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Order.onDeleteItem(data);
        }else if (layEvent === 'orderDetail') {
            Order.openDetailDlg(data);
        }
    });
    //表格行双击
    table.on('rowDouble('+ Order.tableId +')', function(obj){
        console.log(obj.tr) //得到当前行元素对象
        console.log(obj.data) //得到当前行数据
        //obj.del(); //删除当前行
        Order.openEditDlg(obj.data);
    })

});
